import { BasicColumn, BasicTable, FormSchema, useRender, useTable } from "@/components/Table";
import { message, Tag } from "antd";
import { EqpExpiresApi } from "@/api/erp/equipments/destruction";
import { useTranslation } from "react-i18next";
import Toolbar from "@/components/ViewComponents/Toolbar";
import CreateAndUpdata from "./components/createAndUpdata";
import { useDrawer } from "@/components/Drawer";
import { exportToExcel } from "@/utils/util";
import { IconEnum } from "@/enums/appEnum";
import TableAction from "@/components/Table/components/TableAction";
const index = () => {
	const { t } = useTranslation();
	const en_vi = "drugStorage.equipment.destruction";
	const schemas: FormSchema[] = [
		{
			label: t(`drugStorage.equipment.device.eqpName`),
			field: "eqpName",
			component: "Input"
		},
		{
			label: t(`${en_vi}.expireReason`),
			field: "expireReason",
			component: "Input"
		},
		{
			label: t(`${en_vi}.expireProcess`),
			field: "expireProcess",
			component: "Input"
		},
		{
			label: t(`${en_vi}.processId`),
			field: "processId",
			component: "Input"
		},
		{
			label: t("felix_ui.status"),
			component: "Select",
			field: "status",
			componentProps: {
				options: [
					{ label: t(`drugStorage.notStarted`), value: "-1" },
					{ label: t(`drugStorage.inApproval`), value: "1" },
					{ label: t(`drugStorage.approved`), value: "2" },
					{ label: t(`drugStorage.approvalNotApproved`), value: "3" },
					{ label: t(`drugStorage.cancelled`), value: "4" }
				]
			}
		}
	];
	const columns: BasicColumn[] = [
		{
			title: t(`drugStorage.equipment.trial.pkId`),
			dataIndex: "pkId"
		},
		{
			title: t(`drugStorage.equipment.device.eqpName`),
			dataIndex: "eqpName"
		},
		{
			title: t(`${en_vi}.expireDtime`),
			dataIndex: "expireDtime",
			render: text => useRender.renderDate(text)
		},
		{
			title: t(`${en_vi}.expireReason`),
			dataIndex: "expireReason"
		},
		{
			title: t(`${en_vi}.expireProcess`),
			dataIndex: "expireProcess"
		},
		{
			title: t(`${en_vi}.processId`),
			dataIndex: "processId"
		},
		{
			title: t(`drugStorage.createdBy`),
			dataIndex: "createdByName"
		},
		{
			title: t("felix_ui.status"),
			dataIndex: "status",
			render: (_, { status }) => {
				switch (status) {
					case "-1":
						return <Tag color="default">{t(`drugStorage.notStarted`)}</Tag>;
					case "1":
						return <Tag color="processing">{t(`drugStorage.inApproval`)}</Tag>;
					case "2":
						return <Tag color="success">{t(`drugStorage.approved`)}</Tag>;
					case "3":
						return <Tag color="warning">{t(`drugStorage.approvalNotApproved`)}</Tag>;
					case "4":
						return <Tag color="error">{t(`drugStorage.cancelled`)}</Tag>;
					default:
						break;
				}
			}
		},
		{
			title: t("felix_ui.action"),
			dataIndex: "action",
			fixed: "right",
			render: (_: any, record: any) => (
				<TableAction
					actions={[
						{
							icon: IconEnum.EDIT,
							label: t("felix_ui.edit"),
							onClick: handleEdit.bind(null, record)
						},
						{
							icon: IconEnum.DELETE,
							danger: true,
							label: t("felix_ui.delete"),
							popConfirm: {
								title: t("felix_ui.delMessage"),
								placement: "top",
								confirm: handleDelete.bind(null, record.pkId)
							}
						}
					]}
				></TableAction>
			)
		}
	];
	const [registerTable, { reload }] = useTable({
		api: EqpExpiresApi.getEqpExpiresPage,
		columns,
		formConfig: {
			labelWidth: 150,
			labelWrap: true,
			schemas,
			showActionButtonGroup: true,
			showAdvancedButton: false,
			baseColProps: { span: 6 }
		},
		bordered: true,
		useSearchForm: true
	});
	const [registerDrawer, { openDrawer }] = useDrawer();
	const handleEdit = (record: any) => {
		console.log("record", record);
		openDrawer(true, {
			reload,
			record,
			isUpdated: true
		});
	};
	//表格导出
	const exportExcel = async () => {
		await EqpExpiresApi.getEqpExpiresPage({}).then(res => {
			exportToExcel(res.list, t("felix_ui.pat_basic_info"));
		});
	};
	//删除角色
	const handleDelete = async (id: any) => {
		// 发起删除
		const res = await EqpExpiresApi.deleteEqpExpires(id);
		reload();
		if (!res) return message.error(t("viewComponents.patientInformation.deleteFailed"));
		message.success(t("viewComponents.patientInformation.deleteSuccessfully"));
	};
	const handlerToolbarAction = async (key: string) => {
		switch (key) {
			case "add":
				openDrawer(true, {
					reload,
					isUpdated: false
				});
				break;
			case "export":
				exportExcel();
				break;
		}
	};
	return (
		<div className="page">
			<div className="page-content">
				<BasicTable register={registerTable}></BasicTable>
				<CreateAndUpdata register={registerDrawer}></CreateAndUpdata>
			</div>
			<Toolbar action={handlerToolbarAction} btnList={["add", "export"]} />
		</div>
	);
};
export default index;
